package ru.spb.osll.myfit.fixtures;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import ru.spb.osll.myfit.fixtures.base.MyFitTableFixture;
import ru.spb.osll.myfit.server.Configurator;
import ru.spb.osll.myfit.server.parser.PageUtils;
import ru.spb.osll.myfit.util.IOUtils;
import ru.spb.osll.myfit.util.Pair;
import fit.Parse;

public class VerifyPageFixture extends MyFitTableFixture {
	@Override
	public void doStaticTable() {
		List<String> page = readPage();
		// System.out.println(page);
		int actualLines = page.size();
		int expectedLines = getCells().size();

		if (expectedLines + 1 != actualLines) {
			wrong(getCells().get(0).get(0).getSecond(), "expected "
					+ expectedLines + " lines but got " + actualLines);
		}

		Iterator<String> actualIter = page.iterator();
		Iterator<LinkedList<Pair<String, Parse>>> expectedIter = getCells()
				.iterator();
		int lineNumber = 0;
		String actualLine = actualIter.next();
		if(!actualLine.startsWith("!path")) {
			wrong(getCells().get(0).get(0).getSecond(), "missing !path declaration");
		}
		
		while (expectedIter.hasNext()) {
			LinkedList<Pair<String, Parse>> expectedLine = expectedIter.next();
			actualLine = actualIter.next();
			if (expectedLine.size() > 0) {
				String expectedLineStr = "";
				// if (expectedLine.size() == 1
				// && expectedLine.get(0).getFirst().startsWith("!path")) {
				// continue;
				expectedLineStr = expectedLine.get(0).getFirst();
				// } else {
				expectedLineStr = lineToString(expectedLine);
				// }
				if (!expectedLineStr.endsWith(actualLine)) {
					if (expectedLine.size() > 0) {
						wrong(expectedLine.get(0).getSecond(), actualLine);
					} else {
						wrong(getCells().get(0).get(0).getSecond(), "line "
								+ lineNumber + "expected " + expectedLineStr
								+ " but got " + actualLine);
					}
				} else {
					for (Pair<String, Parse> pair : expectedLine) {
						right(pair.getSecond());
					}
				}
			}
			lineNumber++;
		}
	}

	public String lineToString(LinkedList<Pair<String, Parse>> line) {
		String result = "|";
		for (Pair<String, Parse> cell : line) {
			result = result + cell.getFirst() + "|";
		}
		if (result.equals("|")) {
			result = "||";
		}
		return result;
	}

	public List<String> readPage() {
		FileInputStream stream = null;
		List<String> result = new LinkedList<String>();
		try {
			stream = new FileInputStream(PageUtils.getFullPath(
					Configurator.getInstance().getRoot(), ""));
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					stream));
			result = IOUtils.readLines(reader);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (stream != null) {
				try {
					stream.close();
				} catch (IOException e) {
				}
			}
		}

		return result;
	}

	public static String[] getTestTableTemplates() {
		return new String[] { "|verify page|\n||\n" };
	}

}
